package com.pedro.encoder.input.gl.render.filters;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.pedro.encoder.R;
import com.pedro.encoder.utils.gl.GlUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * 基础的运镜功能，缩放，上下，左右抖动
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class ZoomFilter extends BaseFilterRender{

    private final float[] squareVertexDataFilter = {
            // X, Y, Z, U, V
            -1f, -1f, 0f, 0f, 0f, //bottom left
            1f, -1f, 0f, 1f, 0f, //bottom right
            -1f, 1f, 0f, 0f, 1f, //top left
            1f, 1f, 0f, 1f, 1f, //top right
    };
    private int program = -1;
    private int aPositionHandle = -1;
    private int aTextureHandle = -1;
    private int uMVPMatrixHandle = -1;
    private int uSTMatrixHandle = -1;

    private int uTime = -1;
    private int uDuration = -1;
    private int uMaxAmplitude = -1;
    private int uMaxDisplacement = -1;
    private int uMaxVerticalDisplacement = -1;
    private int uFrequency = -1;
    private int uSamplerHandle = -1;



    //parm
    float pUTime=1000.0f;
    long startTime=0;

    float mDration=2f;
    float mMaxAmplitude=0.5f;
    float mMaxDisplacement=0.0f;
    float mMaxVerticalDisplacement=0.0f;
    float mFrequency=0.7f;


    public ZoomFilter(){
        squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        squareVertex.put(squareVertexDataFilter).position(0);
        Matrix.setIdentityM(MVPMatrix, 0);
        Matrix.setIdentityM(STMatrix, 0);
    }
    @Override
    protected void initGlFilter(Context context) {
        String vertexShader = GlUtil.getStringFromRaw(context, R.raw.zoom_vertex);
        String fragmentShader =GlUtil.getStringFromRaw(context, R.raw.simple_fragment);
        program = GlUtil.createProgram(vertexShader, fragmentShader);
        aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
        aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
        uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
        uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");

        uTime = GLES20.glGetUniformLocation(program, "uTime");
        uDuration = GLES20.glGetUniformLocation(program, "uDuration");
        uMaxAmplitude = GLES20.glGetUniformLocation(program, "uMaxAmplitude");
        uMaxDisplacement = GLES20.glGetUniformLocation(program, "uMaxDisplacement");
        uMaxVerticalDisplacement = GLES20.glGetUniformLocation(program, "uMaxVerticalDisplacement");
        uFrequency = GLES20.glGetUniformLocation(program, "uFrequency");

        uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
        startTime = SystemClock.elapsedRealtime();

    }


    @Override
    protected void drawFilter() {
        GLES20.glUseProgram(program);

        squareVertex.position(SQUARE_VERTEX_DATA_POS_OFFSET);
        GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT, false,
                SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
        GLES20.glEnableVertexAttribArray(aPositionHandle);

        squareVertex.position(SQUARE_VERTEX_DATA_UV_OFFSET);
        GLES20.glVertexAttribPointer(aTextureHandle, 2, GLES20.GL_FLOAT, false,
                SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
        GLES20.glEnableVertexAttribArray(aTextureHandle);

        GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0);
        GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);
        GLES20.glUniform1i(uSamplerHandle, 4);
        //
        float currentTime = SystemClock.elapsedRealtime();
        if (currentTime-startTime>30){
            startTime=SystemClock.elapsedRealtime();
            pUTime=pUTime+1;
        }

        GLES20.glUniform1f(uTime, pUTime);
        GLES20.glUniform1f(uDuration, mDration);
        GLES20.glUniform1f(uMaxAmplitude, mMaxAmplitude);
        GLES20.glUniform1f(uMaxDisplacement, mMaxDisplacement);
        GLES20.glUniform1f(uMaxVerticalDisplacement, mMaxVerticalDisplacement);
        GLES20.glUniform1f(uFrequency, mFrequency);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);

    }

    @Override
    public void release() {
        GLES20.glDeleteProgram(program);
    }

//    /**
//     * 周期 1-100
//     * @param dration
//     */
//    public void setmDration(float dration) {
//        this.mDration = dration/100*10;
//    }

    /**
     * 最大振幅 1-100
     * @param maxAmplitude
     */
    public void setmMaxAmplitude(float maxAmplitude) {
        this.mMaxAmplitude = maxAmplitude/100;
    }

    /**
     * 水平晃动的幅度 1-100
     * @param maxDisplacement
     */
    public void setmMaxDisplacement(float maxDisplacement) {
        this.mMaxDisplacement = maxDisplacement/100;
    }

    /**
     * 垂直晃动的幅度 1-100
     * @param maxVerticalDisplacement
     */
    public void setmMaxVerticalDisplacement(float maxVerticalDisplacement) {
        this.mMaxVerticalDisplacement = maxVerticalDisplacement/100;
    }

    /**
     * 速度 1-100
     * @param frequency
     */
    public void setmFrequency(float frequency) {
        this.mFrequency = frequency/100;
    }
   /**
     * 时长1~100，大于100也可以，就是更慢
     * @param dration
     */
    public void setDration(float dration) {
        this.mDration = dration;
    }

}
